package com.soft.sitegenerator.business.hibernate;

import java.util.Collection;

import com.soft.sitegenerator.dao.HibernateDAO;
import com.soft.sitegenerator.exceptions.ObjectNotFoundException;

public class Template {

	private Long id;

	private String name;

	private String canUpdate;

	private User owner;

	private User userComplete;

	private CompositeTemplate compositeTemplate;

	private SitePage sitePage;

	protected HibernateDAO hibernateDAOHib;

	public SitePage getSitePage() {
		return sitePage;
	}

	public void setSitePage(SitePage sitePage) {
		this.sitePage = sitePage;
	}

	public CompositeTemplate getCompositeTemplate() {
		return compositeTemplate;
	}

	public void setCompositeTemplate(CompositeTemplate compositeTemplate) {
		this.compositeTemplate = compositeTemplate;
	}

	public String getCanUpdate() {
		return canUpdate;
	}

	public void setCanUpdate(String canUpdate) {
		this.canUpdate = canUpdate;
	}

	public User getOwner() {
		return owner;
	}

	public void setOwner(User owner) {
		this.owner = owner;
	}

	public User getUserComplete() {
		return userComplete;
	}

	public void setUserComplete(User userComplete) {
		this.userComplete = userComplete;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	// HIBERNATE DAO
	public HibernateDAO getHibernateDAOHib() {
		return hibernateDAOHib;
	}

	public void setHibernateDAOHib(HibernateDAO hibernateDAOHib) {
		this.hibernateDAOHib = hibernateDAOHib;
	}

	public void load() throws ObjectNotFoundException {
		hibernateDAOHib.load(this, this.getId());
	}

	public void save() {
		hibernateDAOHib.save(this);
	}

	public void update() {
		hibernateDAOHib.update(this);
	}

	public void delete() {
		hibernateDAOHib.delete(this);
	}

	public void clonar() {
		hibernateDAOHib.save(this);
	}

	public boolean exist() {
		try {
			hibernateDAOHib.exist(this, this.getId());
		} catch (ObjectNotFoundException e) {
			return false;
		}
		return true;
	}

	public Collection findAll() {
		return hibernateDAOHib.find("from Template order by name");
	}

	public Collection findAllNotModificable() {
		return hibernateDAOHib.find("from Template where canUpdate = 'no' order by name");
	}

	public Collection findAllAsignned() {
		return hibernateDAOHib.find("from Template where userComplete is not NULL and compositeTemplate is NULL order by name");
	}

}
